//
//  Contact.swift
//  HandOffTest
//
//  Created by Lyy on 15/8/19.
//  Copyright (c) 2015年 Lyy. All rights reserved.
//

import UIKit

class Contact: NSObject {
    
    var firstname: String?
    var lastname: String?
    var phoneNumber: String?
    var email: String?
    let documentsDirectory: String?
    
    override init() {
        let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
        documentsDirectory = paths
    }
    
    func getDictionaryFromContactData() -> NSDictionary {
        
        var dictionary: [String: String] = ["firstname": firstname!, "lastname": lastname!, "phoneNumber": phoneNumber!, "email": email!]
        return dictionary
    }
    
    func getContactDataFromDictionary(dictionary: NSDictionary) {
        firstname = dictionary["firstname"] as? String
        lastname = dictionary["firstname"] as? String
        phoneNumber = dictionary["phoneNumber"] as? String
        email = dictionary["email"] as? String

    }
//    更新联系人
    class func updateSavedContacts(contacts: NSMutableArray) {
        let documentsDictionary = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! NSString
        let contactsFilePath = documentsDictionary.stringByAppendingPathComponent("contacts")
        var contactsConverted = NSMutableArray()
        for var i=0; i<contacts.count; ++i {
            contactsConverted.addObject(contacts.objectAtIndex(i).getDictionaryFromContactData())
        }
        contactsConverted.writeToFile(contactsFilePath, atomically: true)
    }

}

extension Contact {
//    保存联系人信息
    func saveContact() {
        let contactsFilePath = documentsDirectory?.stringByAppendingPathComponent("contacts")
        
        var allContacts = loadContacts()
        
        allContacts.addObject(self)
        
        var allContactsConverted = NSMutableArray()
        
        for var i=0; i < allContacts.count; ++i{
            allContactsConverted.addObject(allContacts.objectAtIndex(i).getDictionaryFromContactData())
        }
        
        allContactsConverted.writeToFile(contactsFilePath!, atomically: false)
    }
//    从本地读取联系人
    func loadContacts() -> NSMutableArray {
        let contactsFilePath = documentsDirectory?.stringByAppendingPathComponent("contacts")
        
        var allContacts = NSMutableArray()
        
        if NSFileManager.defaultManager().fileExistsAtPath(contactsFilePath!) {
            let savedContactsArray = NSMutableArray(contentsOfFile: contactsFilePath!)
            
            for var i=0; i < savedContactsArray?.count; ++i{
                let tempContact = Contact()
                let dictionary = savedContactsArray?.objectAtIndex(i) as! NSDictionary
                tempContact.getContactDataFromDictionary(dictionary)
                allContacts.addObject(tempContact)
            }
        }
        
        return allContacts
    }
}


